/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-16 10:38:06
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const Controller = require('egg').Controller;
class tagListsController extends Controller {
    // 查询全表
    async tagLists() {
        const { ctx } = this;
        const tagList = await this.app.mysql.select('tagLists');
        if (tagList) {
            ctx.body = {
                code: 200,
                msg: '获取成功',
                list: tagList
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '获取失败',
            }
        }
    }
    // 添加标签
    async AddTagLists() {
        let { ctx } = this;

        let { title, classValue } = ctx.request.body;
        console.log(title, classValue)
        const post = await this.app.mysql.get('tagLists', { title });
        if (post) {
            ctx.body = {
                msg: '已存在',
                code: 404
            }
        } else {
            const list = await this.app.mysql.insert('tagLists', { title, classValue });

            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '添加成功',
                    list: list
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '添加失败',
                }
            }
        }

    }

    // 删除标签
    async deleteTagLists() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('tagLists', { id });
        if (result) {
            const list = await this.app.mysql.select('tagLists');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '删除失败',
            }
        }



    }


}

module.exports = tagListsController;
